
#include <assert.h>
#include <stdio.h>
#include "hzqFormat.h"


/* 消息头长度结构体，记录各字段长度 */
struct MsgLen Msg = {sizeof(short),sizeof(unsigned int),sizeof(short)+sizeof(unsigned int)};

/**
 *  @brief  打包文件的头6个字节
 *
 */
void  package_head(unsigned char *send_cmd,unsigned short cmd_num,unsigned int packet_len)
{
    int i=0;
    send_cmd[i++] = cmd_num & 0xff;
    send_cmd[i++] = (cmd_num >>8) & 0xff;
	send_cmd[i++] = packet_len & 0xff;
	send_cmd[i++] = (packet_len >> 8 ) & 0xff;
	send_cmd[i++] = (packet_len >> 16) & 0xff;
	send_cmd[i++] = (packet_len >> 24) & 0xff;
}

/** 
 * @brief  解析包头
 * @note   暂时没有考虑大小端问题
 * @param  *p: 
 * @param  *cmd_num: 
 * @param  *packet_len: 
 * @retval None
 */
void head_analyze(char *p,unsigned short *cmd_num,unsigned int *packet_len)
{
    assert(p != NULL);

    *cmd_num=(p[0]&0xff) | ((p[1]<<8)&0xff);
    *packet_len= (p[2]&0xff)                 | 
                ( (p[3]<<8)&0xff00  )       |
                ((p[4]<<16)&0xff0000  )     |
                ((p[5]<<24)&0xff000000);
}